package com.freewater.springboot.demo.aop.apectj.ltwexample;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StopWatch;

@Aspect
public class ProfilingAspect {

    @Pointcut("execution(* com.freewater.springboot.demo.aop.apectj.ltwexample.service..*.*(..))")
    public void methodToBeProfiled() {

    }

    @Around("methodToBeProfiled()")
    public Object profile(ProceedingJoinPoint joinPoint) throws Throwable {
        final StopWatch stopWatch = new StopWatch(getClass().getSimpleName());
        try {
            stopWatch.start(joinPoint.getSignature().getName());
            return joinPoint.proceed();
        } finally {
            stopWatch.stop();
            System.out.println(stopWatch.prettyPrint());
        }
    }

    /**
     * AspectJ 会使用此方法初始化ProfilingAspect对象
     */
    public static ProfilingAspect aspectOf() {
        return new ProfilingAspect();
    }
}
